package com.cy.pj.goods.controller;


import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @GetMapping(value={"/goods/findGoods", "/goods/findGoods/{name}"})
    public String doFindGoods(@PathVariable(required = false) String name, Model model){

        List<Goods> list = goodsService.findGoods(name);
        model.addAttribute("list", list);

        return "/goods/goods";
    }

    @GetMapping("/goods/toInsert")
    public String toInsert(){
        return "/goods/toInsert";
    }

    @PostMapping("/goods/insertGoods")
    public String insertGoods(Goods goods, Model model){

        goodsService.insertGoods(goods);

        List<Goods> list = goodsService.findGoods(null);
        model.addAttribute("list", list);

        return "/goods/goods";
    }

    @GetMapping("/goods/deleteGoodsById/{id}")
    public String deleteGoodsById(@PathVariable (required = false) Integer id, Model model){

        goodsService.deleteGoods(id);

        List<Goods> list = goodsService.findGoods(null);
        model.addAttribute("list", list);

        return "/goods/goods";
    }

    @PostMapping("/goods/UpdateGoods")
    public String updateGoods(Goods goods, Model model){
        System.out.println(goods);
        goodsService.updateGoods(goods);

        List<Goods> list = goodsService.findGoods(null);
        model.addAttribute("list", list);

        return "/goods/goods";
    }

}
